package functionInteface;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * JDK自带的4大函数式接口
 */
public class Demo01 {
    public static void main(String[] args) {
        consumer("as", (string) -> {
            System.out.println(string + ",consumer");
        });

        String result = supplier(() -> {
            return "abc";
        });
        System.out.println(result);

        Integer result1 = function(1, (t) -> {
            return t + 1;
        });
        System.out.println(result1);

        boolean result2 = predicate(10, (t) -> {
            return t > 10;
        });
        System.out.println(result2);
    }

    //1.Consumer<T>:对传入的对象进行操作
    public static void consumer(String string, Consumer<String> consumer) {
        consumer.accept(string);
    }

    //2.Supplier<T>:返回一个对象
    public static <T> T supplier(Supplier<T> supplier) {
        return supplier.get();
    }

    //3.Function<T,R>:传入T对象，返回R对象
    public static <T, R> R function(T t, Function<T, R> function) {
        return function.apply(t);
    }

    //4.Predicate<T>:判断T类型的对象是否满足条件
    public static <T> boolean predicate(T t, Predicate<T> predicate) {
        return predicate.test(t);
    }
}
